package com.oceansoft.mobile.econsole.survey.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oceansoft.mobile.econsole.common.util.StringUtils;
import com.oceansoft.mobile.econsole.common.util.web.PageUtil;
import com.oceansoft.mobile.econsole.exam.service.impl.ExamService;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.survey.entity.*;
import com.oceansoft.mobile.econsole.survey.persistence.ISurveyMapper;
import com.oceansoft.mobile.econsole.survey.service.ISurveyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liujie on 2016/9/1.
 */
@Service
public class SurveyService implements ISurveyService {
    @Resource
    ISurveyMapper surveyMapper;

    @Override
    public Pager<Survey> findSurveyByCondition(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<Survey> page = (Page<Survey>) surveyMapper.findSurveyByCondition(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    public List<Survey> findSurveysByCondition(Query query) {
        return surveyMapper.findSurveyByCondition(query);
    }

    @Override
    public Survey getSurveyById(int id) {
        return surveyMapper.getSurveyById(id);
    }

    @Override
    public Result saveSurvey(Survey survey) throws Exception {
        if (survey.getId() == Integer.MIN_VALUE) {
            surveyMapper.newSurvey(survey);
        } else {
            surveyMapper.updateSurvey(survey);
        }
        return new Result("保存成功");
    }

    @Override
    public Result deleteSurvey(int id) throws Exception {
        surveyMapper.deleteSurvey(id);
        return new Result("删除成功");
    }

    @Override
    public Pager<Question> findQuestionByCondition(Query query) {
        PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
        Page<Question> page = (Page<Question>) surveyMapper.findQuestionByCondition(query);
        return PageUtil.page2pager(page, query);
    }

    @Override
    public Question getQuestionById(int id) {
        return surveyMapper.getQuestionById(id);
    }

    @Override
    public Result saveQuestion(Question question, HttpServletRequest request) throws Exception {
        if(question.getId()==Integer.MIN_VALUE){
            surveyMapper.newQuestion(question);
        }else{
            surveyMapper.updateQuestion(question);
        }
        surveyMapper.deleteOptions(question.getId());//先清除选项
        int count=0;
        if(StringUtils.isNotEmpty(request.getParameter("count"))){
            count = Integer.parseInt(request.getParameter("count"));
        }
        for(int i=1;i<=count;i++){
            if(StringUtils.isNotEmpty(request.getParameter("name"+i))){
                //保存选项
                Option o = new Option();
                o.setLabel(ExamService.labelMap.get(i));
                o.setName(request.getParameter("name" + i));
                o.setQuestionid(question.getId());
                surveyMapper.newOption(o);
            }
        }
        return new Result("保存成功");
    }

    @Override
    public Result deleteQuestion(int id) throws Exception {
        surveyMapper.deleteQuestion(id);
        surveyMapper.deleteOptions(id);
        return new Result("删除成功");
    }

    @Override
    public List<Option> getOptionsByQuestion(Question question) {
        return surveyMapper.getOptionsByQuestion(question);
    }

    @Override
    public List<QuestionExt> getQuestionsBySurvey(Survey survey) {
        List<QuestionExt> list = surveyMapper.getQuestionsBySurvey(survey);
        for(QuestionExt question : list){
            question.setOptions(surveyMapper.getOptionsByQuestion(question));
        }
        return list;
    }

    @Override
    public Result saveAnswers(List<Answer> answerList) throws Exception {
        for(Answer answer:answerList){
            surveyMapper.saveAnswer(answer);
        }
        return new Result("保存成功");
    }

}
